package com.q.tqb.adapter;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.Request;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.target.SizeReadyCallback;
import com.bumptech.glide.request.target.Target;
import com.q.tqb.R;
import com.q.tqb.util.FileUtil;


import uk.co.senab.photoview.PhotoView;
import uk.co.senab.photoview.PhotoViewAttacher;

public class ImagePagerAdapter extends PagerAdapter {

    private String[] images;
    private Activity mActivity;
    private Bitmap mBitmap;

    public ImagePagerAdapter(Activity context, String[] images) {
        this.images = images;
        this.mActivity = context;
    }


    @Override
    public int getCount() {
        return images.length;
    }

    @Override
    public View instantiateItem(ViewGroup container, final int position) {

        View view = View.inflate(mActivity, R.layout.view_image_pager, null);
        final PhotoView photoView = view.findViewById(R.id.pv_image);
        final ProgressBar progressBar = view.findViewById(R.id.progress);

        Glide
                .with(mActivity)
                .load(images[position])
                .asBitmap()
                .into(new SimpleTarget<Bitmap>() {
                    @Override
                    public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                        mBitmap = resource;
                        progressBar.setVisibility(View.GONE);
                        photoView.setImageBitmap(resource);
                    }
                });

        photoView.setZoomable(true);
        photoView.setOnPhotoTapListener(new PhotoViewAttacher.OnPhotoTapListener() {
            @Override
            public void onPhotoTap(View arg0, float arg1, float arg2) {
                mActivity.finish();
            }

            @Override
            public void onOutsidePhotoTap() {
            }
        });

        photoView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {

                mOnItemClickListener.onLongClick(mBitmap);
                return false;
            }
        });

        // container.addView(photoView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        container.addView(view);
        return view;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }


    public interface OnLongClickListener {
        void onLongClick(Bitmap mBitmap);
    }

    private OnLongClickListener mOnItemClickListener;


    public void setOnItemClickListener(OnLongClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

}